package com.hmdp.test.lifecycle.event;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

/**
 * 发布事件, 实现 ApplicationEventPublisherAware 接口增加发布事件的功能
 *
 * @author lizhihui
 */
@Slf4j
@Component
public class StringEventPublisher implements ApplicationEventPublisherAware {

    private static final String EVENT_CONTENT = "12345";

    @SneakyThrows
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        StringEvent stringEvent = new StringEvent(EVENT_CONTENT);

        // 发布事件，发布后会在 ApplicationListener.onApplicationEvent() 方法进行捕获
        // 发布事件
        // TimeUnit.SECONDS.sleep(3);
        applicationEventPublisher.publishEvent(stringEvent);

        log.error("EVENT.发布事件，事件对象为 StringEvent，内容={}", EVENT_CONTENT);
    }

}
